package com.example.demo.controller;

import cn.hutool.core.convert.Convert;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.demo.entity.WaresManage;
import com.example.demo.service.WaresManageService;
import com.example.demo.utils.JsonResult;
import com.example.demo.utils.ResultUtil;
import com.example.demo.utils.TimeUtil;
import com.example.demo.utils.UserInfoUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

/**
 * 货物管理控制
 */
@RestController
@RequestMapping(value = "/WaresManage")
public class WaresManageController {
    @Autowired
    private WaresManageService waresManageService;

    /**
     * 分页查询
     * @return
     */
    @RequestMapping(value = "/getWaresManagePage",method = RequestMethod.GET)
    public JsonResult getWaresManagePage(@RequestParam(value = "current") Integer current,
                                         @RequestParam(value = "size")Integer size){
        IPage<List<Map<String, String>>> waresManagePage = waresManageService.getWaresManagePage(new Page<>(current, size));
        return ResultUtil.success("查询成功",waresManagePage);
    }

    /**
     * 添加
     * @param waresManage
     * @return
     */
    @RequestMapping(value = "/addWaresManage",method = RequestMethod.POST)
    public JsonResult addWaresManage(@RequestBody WaresManage waresManage){
        String userName = UserInfoUtil.gerUserName();
        String dateTime = TimeUtil.getDateTime();

        waresManage.setByName(userName);
        waresManage.setCreateDate(dateTime);
        waresManage.setDel(0);

        boolean save = waresManageService.save(waresManage);
        if(save){
            return ResultUtil.success("添加成功");
        }else{
            return ResultUtil.fail("添加失败");
        }
    }

    /**
     * 删除
     * @param
     * @return
     */
    @RequestMapping(value = "/delWaresManage/{id}",method = RequestMethod.DELETE)
    public JsonResult delWaresManage(@PathVariable Integer id){
        boolean b = waresManageService.removeById(id);
        if(b){
            return ResultUtil.success("删除成功");
        }else{
            return ResultUtil.fail("删除失败");
        }
    }

    /**
     * 修改
     * @return
     */
    @RequestMapping(value = "/editWaresManage",method = RequestMethod.PUT)
    public JsonResult editWaresManage(@RequestBody WaresManage waresManage){
        String userName = UserInfoUtil.gerUserName();
        String dateTime = TimeUtil.getDateTime();

        waresManage.setEditName(userName);
        waresManage.setUpdateDate(dateTime);

        boolean b = waresManageService.updateById(waresManage);
        if (b){
            return ResultUtil.success("修改成功");
        }else{
            return ResultUtil.fail("修改失败");
        }
    }

    /**
     *获取列表
     * @return
     */
    @RequestMapping(value = "/getWaresManageList",method = RequestMethod.GET)
    public JsonResult getWaresManageList(){
        return  ResultUtil.success("查询成功",waresManageService.list(Wrappers.query(null)));
    }
}
